// pages/shangpin_xq/shangpin_xq.js
const app = getApp()
//获得全局js变量
var util = require('../../utils/util.js');

var WxParse = require('../../wxParse/wxParse.js');
var replyHtml0 = ``;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgurl:app.data.imgurl,
        list: [],
        indicatorDots: true,
        vertical: false,
        autoplay: false,
        interval: 2000,
        duration: 500,
        userId: 0,//用户信息
        shoucang:true,
        id:0,
        detail:[],
        num:1,
        guigeid:0,
        guigeindex:0,
        guige:[],
        gprice:0,
        price:0,
        isShow:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        
        var userId = app.globalData.userId;
        this.setData({
            id: options.id,
            userId: userId
        });
        //获取商品详情
        this.goodsdetail();
    },
    
    changeguige:function(e){
        var id=e.currentTarget.dataset.id;
        console.log(id);
        this.setData({
            guigeindex:id,
            guigeid:this.data.guige[id]['id'],
            gprice:this.data.guige[id]['price']
        });
    },
    //获取商品详情
    goodsdetail:function(){
        var that = this;
        var userid=this.data.userId;
        util.httpRrquest(app.data.rooturl + 'mall/getGoodDetailById', 'GET', { id: that.data.id, userId: userid})
        .then(function (response) {
            var data = response.data.data;
            that.setData({
                list: data.goodImgsSrc,
                detail: data,
                shoucang: data.collect,
                guige: data.spec,
                guigeid:data.spec[0]['id'],
                gprice:data.spec[0]['price']
            })
            
            WxParse.wxParse('content', 'html', data.goodDetail, that, 10);
            wx.setNavigationBarTitle({
                title: data.goodName
            })
        }, function (error) {
            console.log(error);
        });
    },
    addcart:function(){
        var that = this;
        var userid = this.data.userId;
        util.httpRrquest(app.data.rooturl + 'mall/addCart', 'POST', { id: that.data.id, userid: userid,num:this.data.num,ggid:this.data.guigeid })
        .then(function (response) {
            wx.showToast({
                title: response.data.msg,
                icon: 'none'
            })
        }, function (error) {
            console.log(error);
        });
    },
    add:function(){
        var num=this.data.num
        num++;
        this.setData({
            num:num
        });
    },
    dec:function(){
        var num=this.data.num;
        if(num<=1){
            return;
        }
        num--;
        this.setData({
            num:num
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
    },
    //收藏
    shouCang(){
        var that = this;
        var userid=this.data.userId;
        util.httpRrquest(app.data.rooturl + 'mall/goodCollect', 'GET', { id: that.data.id,userId:userid,shoucang:this.data.shoucang })
        .then(function (response) {
            console.log(response.data);
            wx.showToast({
                title: response.data.msg,
                icon: 'none'
            })
            that.setData({
                shoucang: response.data.data
            })
        }, function (error) {
            console.log(error);
        });
    },
    lianxi_kefu(){
        if(this.data.isshow){
            this.setData({
                isshow:false
            })
        }else{
            this.setData({
                isshow:true
            })
        }
    },
    ljlingqu:function(){
        this.setData({
            isShow:true
        });
    },
    cardcancel:function(){
        this.setData({
            isShow:false
        });
    },
    cardcheck:function(){
        var userid=wx.getStorageSync('user_id');
        var that = this;
        util.httpRrquest(app.data.rooturl + 'user/getdalibao', 'POST', { g_id: that.data.g_id, userid:userid, name:that.data.name,phone:that.data.phone,address:that.data.address})
        .then(function (response) {
            wx.showToast({
                title: response.data.msg,
                icon: 'none',
                duration:2000
            })
            that.setData({
                isShow:false
            });
        }, function (error) {
            console.log(error);
        });
    }
})